package com.mobile.challenge.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @Author Touge
 * @Date 2020/9/17 16:47
 * @Description
 */
public class DateUtil {

    public static String getFormatDateStr(Calendar calendar) {
        return getFormatDateStr(calendar.getTimeInMillis());
    }

    private static String getFormatDateStr(long time) {
        return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(time);
    }

    public static String getFormatDateMonthStr(Calendar calendar) {
        return getFormatDateStr(calendar.getTimeInMillis());
    }

    /**
     * 月份格式化
     *
     * @param time
     * @return
     */
    private static String getFormatDateMonthStr(long time) {
        return new SimpleDateFormat("yyyy-MM", Locale.getDefault()).format(time);
    }

    /**
     * 获取 七天前的日期
     */

    public static String getFormatWeekStartData(Calendar calendar) {
        long timeInMillis = calendar.getTimeInMillis();
        timeInMillis = timeInMillis - 1000 * 60 * 60 * 24 * 7;
        return getFormatDateStr(timeInMillis);
    }

    /**
     * 获取 当月 1号
     */
    public static String getFormatMonthStartData(Calendar calendar) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(calendar.getTimeInMillis());
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        return getFormatDateStr(calendar1.getTimeInMillis());
    }

    public static boolean isToday(String dateStr) {
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).parse(dateStr);
            String formatDateStr1 = getFormatDateStr(date.getTime());
            String formatDateStr2 = getFormatDateStr(Calendar.getInstance());
            return formatDateStr1.equals(formatDateStr2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
